package com.yongninger.dao;

import java.util.ArrayList;
import java.util.List;

import com.yongninger.vo.ClerkInfo;
import com.yongninger.vo.CustomerInfo;
import com.yongninger.vo.ProductVo;
import com.yongninger.vo.ShipmentsInfoVo;
import com.yongninger.vo.ShipmentsVo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class ShipmentsDao extends AbstractDao{

	public ShipmentsDao(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * 插入发货单
	 */
	public synchronized boolean insertShipments(ShipmentsVo shipmentsVo){
		boolean state = false;
		ContentValues values = new ContentValues();
		values.put("scode", shipmentsVo.getScode());
		values.put("sclerk", shipmentsVo.getSclerk());
		values.put("sdate", shipmentsVo.getSdate());
		values.put("scustomer1", shipmentsVo.getScustomer1());
		values.put("scustomer2", shipmentsVo.getScustomer2());
		values.put("username", shipmentsVo.getUsername());
		values.put("scompany", shipmentsVo.getScompany());
		values.put("saddress", shipmentsVo.getSaddress());
		values.put("sperson", shipmentsVo.getSperson());
		values.put("scontact", shipmentsVo.getScontact());
		values.put("scontent", shipmentsVo.getScontent());
		values.put("sproductname", shipmentsVo.getSproductname());
		values.put("sproducttype", shipmentsVo.getSproducttype());
		values.put("sproductsum1", shipmentsVo.getSproductsum1());
		values.put("sproductsum2", shipmentsVo.getSproductsum2());
		values.put("sproductprice", shipmentsVo.getSproductprice());
		values.put("sproductcount", shipmentsVo.getSproductcount());
		values.put("wuliuinfo", shipmentsVo.getWuliuinfo());
		values.put("contentnums", shipmentsVo.getContentnums());
		long row = this.insert("shipments", null, values);
		if(row != -1){
			state =  true;
		}
		return state;
	}
	
	
	/**
	 * 更新发货单信息
	 */
	public synchronized boolean updateShipmentsVo(ShipmentsVo shipmentsVo,int id)
	{
		
		ContentValues values = new ContentValues();
		values.put("scode", shipmentsVo.getScode());
		values.put("sclerk", shipmentsVo.getSclerk());
		values.put("sdate", shipmentsVo.getSdate());
		values.put("scustomer1", shipmentsVo.getScustomer1());
		values.put("scustomer2", shipmentsVo.getScustomer2());
		values.put("username", shipmentsVo.getUsername());
		values.put("saddress", shipmentsVo.getSaddress());
		values.put("sperson", shipmentsVo.getSperson());
		values.put("scontact", shipmentsVo.getScontact());
		values.put("scontent", shipmentsVo.getScontent());
		values.put("sproductname", shipmentsVo.getSproductname());
		values.put("sproducttype", shipmentsVo.getSproducttype());
		values.put("sproductsum1", shipmentsVo.getSproductsum1());
		values.put("sproductsum2", shipmentsVo.getSproductsum2());
		values.put("sproductprice", shipmentsVo.getSproductprice());
		values.put("sproductcount", shipmentsVo.getSproductcount());
		values.put("wuliuinfo", shipmentsVo.getWuliuinfo());
		values.put("contentnums", shipmentsVo.getContentnums());
		
		int row = this.update("shipments", values, "sid = ?", new String[] {String.valueOf(id)});
		
		if(row == -1)
			return false;
		
		return true;
	}
	
	
	
	/**
	 * 删除发货单
	 */
	public synchronized boolean deleteShipmentsVo(int id)
	{
		
		int row = this.delete("shipments", "sid = ?", new String[] {String.valueOf(id)});
		if(row == -1)
			return false;
		
		return true;
	} 
	
	/**
	 *查询单个发货单
	 */
	public synchronized ShipmentsVo queryShipmentsVo(int id)
	{
		ShipmentsVo shipmentsVo = new ShipmentsVo();
		Cursor c = this.query("shipments", 
				new String[]{"sid","scode","sdate","sdate","scustomer1","scustomer2","username","saddress"
							,"sperson","scontact","scontent","sproductname","sproducttype"
							,"sproductsum1","sproductsum2","sproductprice","sproductcount","wuliuinfo","contentnums"},
				"sid=?", new String[]{String.valueOf(id)});
		while (c.moveToNext()) {
			shipmentsVo.setSid(c.getInt(c.getColumnIndex("sid")));
		}
		c.close();
		return shipmentsVo;

	}
	
	
	/**
	 * 查询发货单
	 */
	public synchronized List<ShipmentsVo> queryShipmentsVoList(ShipmentsVo shipmentsVoParam,int start,int pagesize)
	{
		
		if(null != shipmentsVoParam){
			String sql = "select sid,scode,sdate,sclerk,scompany,scustomer1,scustomer2,username,saddress"
					+",sperson,scontact,scontent,sproductname,sproducttype"
					+",sproductsum1,sproductsum2,sproductprice,sproductcount,wuliuinfo,contentnums from shipments where 1=1 ";
			if(null != shipmentsVoParam.getScompany() && !"请选择".equals(shipmentsVoParam.getScompany())){
				sql += " and scompany = '" + shipmentsVoParam.getScompany() + "'";
			}
			if(null != shipmentsVoParam.getSclerk() && !"请选择".equals(shipmentsVoParam.getSclerk())){
				sql += " and sclerk = '" + shipmentsVoParam.getSclerk()+"'";
			}
			if(null != shipmentsVoParam.getSdate2() && !"".equals(shipmentsVoParam.getSdate2())){
				sql += " and sdate >= '" + shipmentsVoParam.getSdate2()+"'";
			}
			if(null != shipmentsVoParam.getSdate3() && !"".equals(shipmentsVoParam.getSdate3())){
				sql += " and sdate <= '" + shipmentsVoParam.getSdate3()+"'";
			}
			if(null != shipmentsVoParam.getScustomer1() && !"请选择".equals(shipmentsVoParam.getScustomer1())){
				sql += " and scustomer1 = '" + shipmentsVoParam.getScustomer1()+"'";
			}
			Cursor c = this.query(sql,null);
			List<ShipmentsVo> list = setShipments(c);
			return list;
		}else{
			/*Cursor c = this.query("shipments", 
					new String[]{"sid","scode","sdate","sclerk","scompany","scustomer1","scustomer2","username","saddress"
					,"sperson","scontact","scontent","sproductname","sproducttype"
					,"sproductsum1","sproductsum2","sproductprice","sproductcount"}, null, null);*/
			String sql = "select sid,scode,sdate,sclerk,scompany,scustomer1,scustomer2,username,saddress"
					+",sperson,scontact,scontent,sproductname,sproducttype"
					+",sproductsum1,sproductsum2,sproductprice,sproductcount,wuliuinfo,contentnums from shipments  order by sid desc  limit "+start+" ,"+pagesize;
			Cursor c = this.query(sql,null);
			List<ShipmentsVo> list = setShipments(c);
			return list;
		}
	}

	private List<ShipmentsVo> setShipments(Cursor c) {
		List<ShipmentsVo> list = new ArrayList<ShipmentsVo>();
		while (c.moveToNext()) {
			ShipmentsVo shipmentsVo = new ShipmentsVo();
			shipmentsVo.setSid(c.getInt(c.getColumnIndex("sid")));
			shipmentsVo.setScode(c.getString(c.getColumnIndex("scode")));
			shipmentsVo.setSdate(c.getString(c.getColumnIndex("sdate")));
			shipmentsVo.setSclerk(c.getString(c.getColumnIndex("sclerk")));
			shipmentsVo.setScompany(c.getString(c.getColumnIndex("scompany")));
			shipmentsVo.setScustomer1(c.getString(c.getColumnIndex("scustomer1")));
			shipmentsVo.setScustomer2(c.getString(c.getColumnIndex("scustomer2")));
			shipmentsVo.setUsername(c.getString(c.getColumnIndex("username")));
			shipmentsVo.setSaddress(c.getString(c.getColumnIndex("saddress")));
			shipmentsVo.setSperson(c.getString(c.getColumnIndex("sperson")));
			shipmentsVo.setScontact(c.getString(c.getColumnIndex("scontact")));
			shipmentsVo.setScontent(c.getString(c.getColumnIndex("scontent")));
			shipmentsVo.setSproductname(c.getString(c.getColumnIndex("sproductname")));
			shipmentsVo.setSproducttype(c.getString(c.getColumnIndex("sproducttype")));
			shipmentsVo.setSproductsum1(c.getString(c.getColumnIndex("sproductsum1")));
			shipmentsVo.setSproductsum2(c.getString(c.getColumnIndex("sproductsum2")));
			shipmentsVo.setSproductprice(c.getString(c.getColumnIndex("sproductprice")));
			shipmentsVo.setSproductcount(c.getString(c.getColumnIndex("sproductcount")));
			shipmentsVo.setWuliuinfo(c.getString(c.getColumnIndex("wuliuinfo")));
			shipmentsVo.setContentnums(c.getString(c.getColumnIndex("contentnums")));
			list.add(shipmentsVo);
		}
		c.close();
		return list;
	}
	/**
	 *查询业务员
	 */
	public synchronized List<ClerkInfo>  queryClerkInfoVo()
	{
		List<ClerkInfo> clerkInfosList = new ArrayList<ClerkInfo>();
		Cursor c = this.query("clerk_info", 
				new String[]{"cid","ccode","cname","ccompany"},
				null,null);
		while (c.moveToNext()) {
			ClerkInfo clerkInfo = new ClerkInfo();
			clerkInfo.setCid(c.getInt(c.getColumnIndex("cid")));
			clerkInfo.setCcode(c.getString(c.getColumnIndex("ccode")));
			clerkInfo.setCname(c.getString(c.getColumnIndex("cname")));
			clerkInfo.setCcompany(c.getString(c.getColumnIndex("ccompany")));
			clerkInfosList.add(clerkInfo);
		}
		c.close();
		return clerkInfosList;

	}
	/**
	 *查询发货组织
	 */
	public synchronized List<ShipmentsInfoVo>  queryShipmentsInfoVo()
	{
		List<ShipmentsInfoVo> shipmentsInfoVos = new ArrayList<ShipmentsInfoVo>();
		Cursor c = this.query("shipments_info", 
				new String[]{"sid","sname","scode","scompany"},
				null,null);
		while (c.moveToNext()) {
			ShipmentsInfoVo shipmentsInfoVo = new ShipmentsInfoVo();
			shipmentsInfoVo.setSid(c.getInt(c.getColumnIndex("sid")));
			shipmentsInfoVo.setSname(c.getString(c.getColumnIndex("sname")));
			shipmentsInfoVo.setScode(c.getString(c.getColumnIndex("scode")));
			shipmentsInfoVo.setScompany(c.getString(c.getColumnIndex("scompany")));
			shipmentsInfoVos.add(shipmentsInfoVo);
		}
		c.close();
		return shipmentsInfoVos;

	}
	/**
	 *查询产品
	 */
	public synchronized List<ProductVo>  queryProductInfoVo()
	{
		List<ProductVo> productInfos = new ArrayList<ProductVo>();
		Cursor c = this.query("product_info", 
				new String[]{"pid","pcode","pname","pcategory","ptype","smeasure1","smeasure2","pprice"},
				null,null);
		while (c.moveToNext()) {
			ProductVo productInfo = new ProductVo();
			productInfo.setPid(c.getInt(c.getColumnIndex("pid")));
			productInfo.setPcode(c.getString(c.getColumnIndex("pcode")));
			productInfo.setPname(c.getString(c.getColumnIndex("pname")));
			productInfo.setPcategory(c.getString(c.getColumnIndex("pcategory")));
			productInfo.setPtype(c.getString(c.getColumnIndex("ptype")));
			productInfo.setSmeasure1(c.getString(c.getColumnIndex("smeasure1")));
			productInfo.setSmeasure2(c.getString(c.getColumnIndex("smeasure2")));
			productInfo.setPprice(c.getString(c.getColumnIndex("pprice")));
			productInfos.add(productInfo);
		}
		c.close();
		return productInfos;

	}
	/**
	 *根据业务员选择 客户list
	 */
	public synchronized List<CustomerInfo>  queryCustomerInfoVo(String clerk)
	{
		List<CustomerInfo> customerInfoList = new ArrayList<CustomerInfo>();
		Cursor c = this.query("customer_info", 
				new String[]{"_id","ccode","cname","caddress","cperson","ccontact","cclerk"},
				" cclerk = ? ", new String[] {String.valueOf(clerk)});
		while (c.moveToNext()) {
			CustomerInfo customerInfo = new CustomerInfo();
			customerInfo.setCid(c.getInt(c.getColumnIndex("_id")));
			customerInfo.setCcode(c.getString(c.getColumnIndex("ccode")));
			customerInfo.setCname(c.getString(c.getColumnIndex("cname")));
			customerInfo.setCaddress(c.getString(c.getColumnIndex("caddress")));
			customerInfo.setCperson(c.getString(c.getColumnIndex("cperson")));
			customerInfo.setCcontact(c.getString(c.getColumnIndex("ccontact")));
			customerInfo.setCclerk(c.getString(c.getColumnIndex("cclerk")));
			customerInfoList.add(customerInfo);
		}
		c.close();
		return customerInfoList;

	}
	/**
	 *根据业务员选择 客户list
	 */
	public synchronized List<CustomerInfo>  queryCustomerInfoVo()
	{
		List<CustomerInfo> customerInfoList = new ArrayList<CustomerInfo>();
		Cursor c = this.query("customer_info", 
				new String[]{"_id","ccode","cname","caddress","cperson","ccontact","cclerk"},
				null, null);
		while (c.moveToNext()) {
			CustomerInfo customerInfo = new CustomerInfo();
			customerInfo.setCid(c.getInt(c.getColumnIndex("_id")));
			customerInfo.setCcode(c.getString(c.getColumnIndex("ccode")));
			customerInfo.setCname(c.getString(c.getColumnIndex("cname")));
			customerInfo.setCaddress(c.getString(c.getColumnIndex("caddress")));
			customerInfo.setCperson(c.getString(c.getColumnIndex("cperson")));
			customerInfo.setCcontact(c.getString(c.getColumnIndex("ccontact")));
			customerInfo.setCclerk(c.getString(c.getColumnIndex("cclerk")));
			customerInfoList.add(customerInfo);
		}
		c.close();
		return customerInfoList;

	}
}
